package com.wenwei.sharebox.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.umeng.message.PushAgent;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.UserLoginBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.RogerSPUtils;

import org.json.JSONObject;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * Created by Administrator on 2017/07/12 0012.
 */
public class LoginPopup extends PopupWindow implements View.OnClickListener{

    Activity mActivity;
    PopupWindow popupWindow;

    EditText phoneEt;
    EditText psEt;
    ImageView closeIv;
    TextView forgetTv;
    TextView loginTv;

    public CallBack mCallBack;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_close:
                popupWindow.dismiss();
                break;
            case R.id.forget_tv:
                popupWindow.dismiss();
                mCallBack.onForgetClick();
                break;
            case R.id.login_tv:
                userLogin();
                break;
        }
    }

    public interface CallBack{
        void onForgetClick();
        void onLoginSuccess();
    }


    public LoginPopup(Activity activity, View parentView, CallBack callBack) {
        this.mCallBack = callBack;
        mActivity = activity;
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.custom_login_popup, null);
        phoneEt = (EditText) contentView.findViewById(R.id.input_phone);
        psEt = (EditText) contentView.findViewById(R.id.input_ps);
        closeIv = (ImageView) contentView.findViewById(R.id.iv_close);
        closeIv.setOnClickListener(this);
        forgetTv = (TextView) contentView.findViewById(R.id.forget_tv);
        forgetTv.setOnClickListener(this);
        loginTv = (TextView) contentView.findViewById(R.id.login_tv);
        loginTv.setOnClickListener(this);

        Observable<CharSequence> phoneObservable = RxTextView.textChanges(phoneEt);
        Observable<CharSequence> psObservable = RxTextView.textChanges(psEt);
        Observable.combineLatest(phoneObservable, psObservable, new Func2<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2) {
                return !charSequence.toString().equals("") && !charSequence2.toString().equals("");
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                loginTv.setEnabled(aBoolean);
                if(aBoolean){
                    loginTv.setBackgroundResource(R.drawable.bc_share_select);
                }else{
                    loginTv.setBackgroundResource(R.drawable.bc_click_false);
                }
            }
        });
        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        ColorDrawable cd = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cd);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.4f;

        activity.getWindow().setAttributes(lp);
        popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
    }


    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }


    BaseSubscriber<UserLoginBean> loginSubscriber;
    private void userLogin(){
        String userPhone = phoneEt.getText().toString();
        String userPw = psEt.getText().toString();
        loginSubscriber = new BaseSubscriber<UserLoginBean>(mActivity) {
            @Override
            public void onSuccess(UserLoginBean s) {
                RogerSPUtils.putObject(mActivity, Constant.SP_USER_LOGIN_RESULT,s);
                Toast.makeText(mActivity,"登录成功，立即体验吧",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                mCallBack.onLoginSuccess();
            }
        };
        try {
            JSONObject jb = new JSONObject();
            jb.put("password",userPw);
            jb.put("username",userPhone);
            jb.put("registrationId","12313213123");
            jb.put("platform","android");

            ApiManager.getInstance().userLogin(loginSubscriber,userPhone,userPw,"1231231qa");
        }catch (Exception e){}

    }
}
